﻿using System.Linq;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using System.Collections.ObjectModel;
using BestCode.Creds.Entities;

namespace BestCode.Creds.UI.UserControls
{
    public partial class DynamicCanvas : UserControl
    {
        public DynamicCanvas()
        {
            InitializeComponent();
        }
        
        private float ConvertPixelsToCm(float value)
        {
            return (float) ((value*2.54)/96);
        }

      

        public Control SelectedControlTemp;

        private static readonly DependencyProperty ItemsSourceProperty = DependencyProperty.Register("ItemsSource",
                                                                        typeof(ObservableCollection<Layout>),
                                                                        typeof(DynamicCanvas),
                                                                        new PropertyMetadata(OnItemsSourcePropertyChanged));
        public ObservableCollection<Layout> ItemsSource
        {
            get { return (ObservableCollection<Layout>)GetValue(ItemsSourceProperty); }
            set { SetValue(ItemsSourceProperty, value); }
        }

        private static void OnItemsSourcePropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            DynamicCanvas dc = (DynamicCanvas)d;
            dc.PopulateControlsIntoCanvas();
        }

        public void PopulateControlsIntoCanvas()
        {
            DynamicAreaCanvas.Children.Clear();
            if(ItemsSource!=null)
                foreach (var item in ItemsSource)
                {
                    Button button = new Button();
                    button.Margin = new Thickness(item.X == 0 ? 0 : item.X, item.Y == 0 ? 0 : item.Y, 0, 0);
                    button.Width = item.Width;
                    button.Height = item.Height;
                    button.Content = item.FieldName;
                    button.Tag = item;
                    DynamicAreaCanvas.Children.Add(button);
                    button.Click += button_Click;
                }
        }

        void button_Click(object sender, RoutedEventArgs e)
        {
            var item = (Layout)((Button) sender).Tag;
            SelectedItem = ItemsSource.FirstOrDefault(p => p.Id == item.Id);
        }

        

        //void button_Click(object sender, RoutedEventArgs e)
        //{
        //    SelectedItem = ItemsSource.FirstOrDefault(p => p.ReportTemplateFieldName.Equals(((ReportTemplateFieldValue)((Button)sender).Tag).ReportTemplateFieldName));
        //}

        //void button_LostFocus(object sender, RoutedEventArgs e)
        //{
        //    if ((SelectedItem != null) && (((ReportTemplateFieldValue)((Button)sender).Tag).ReportTemplateFieldName.Equals(SelectedItem.ReportTemplateFieldName)))
        //    {
        //        float CanvasLeft;
        //        float.TryParse(Canvas.GetLeft((Button)sender).ToString(), out CanvasLeft);
        //        float CanvasTop;
        //        float.TryParse(Canvas.GetTop((Button)sender).ToString(), out CanvasTop);

        //        float left, top;
        //        if (float.TryParse(((Button) sender).Margin.Left.ToString(), out left))
        //            SelectedItem.X = (ConvertPixelsToCm(left + CanvasLeft));
        //        if (float.TryParse(((Button) sender).Margin.Top.ToString(), out top))
        //            SelectedItem.Y = (ConvertPixelsToCm(top + CanvasTop));
        //    }
        //}



        private static readonly DependencyProperty SelectedItemProperty = DependencyProperty.Register("SelectedItem",
                                                                        typeof(Layout),
                                                                        typeof(DynamicCanvas),
                                                                        new PropertyMetadata(OnSelectedItemPropertyChanged));
        public Layout SelectedItem
        {
            get { return (Layout)GetValue(SelectedItemProperty); }
            set { SetValue(SelectedItemProperty, value); }
        }

        private static void OnSelectedItemPropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            DynamicCanvas dc = (DynamicCanvas)d;

            dc.SetPropertyIntoControl();
        }
                
        protected UIElement FindControlSelected()
        {
            if (SelectedItem != null)
            {
                foreach (var item in DynamicAreaCanvas.Children)
                {
                    if (item.GetType() == typeof(Button))
                    {
                        var tag = (Layout)((Button)item).Tag;
                        if (tag.FieldName.Equals(SelectedItem.FieldName))
                        {
                            return item as UIElement;
                        }
                    }
                }
            }
            return null;
        }

        public void SetPropertyIntoControl()
        {
            var control = FindControlSelected();
            if (control != null)
                ((Button) control).Focus();
        }
    }
}
